package 抢票.Test01;

public class SellTicket {
    String name;
    int password;
    int tickets = 100;

    public SellTicket(String name, int password) {
        this.name = name;
        this.password = password;
    }

    public void TicketCollection(int tickets){
        synchronized (this){
            int oldTickets = this.tickets;
            if (tickets > oldTickets){
                System.out.println("票已不足" + tickets + ",账户：" + Thread.currentThread().getName() + "张取票失败!!!");
                return;
            }
            this.tickets = oldTickets - tickets;
            System.out.println(Thread.currentThread().getName() + "取票成功还剩：" + this.tickets);

        }

    }

}
